package com.lb.shopguide.manager.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;

/**
 * Created by kang on 2017/10/24.
 */

public class LoginStatusService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, final int startId) {
        if (!TextUtils.isEmpty(UserConfigManager.getInstance(this).getUserToken())) {
            NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(new Observer<StatusCode>() {
                public void onEvent(StatusCode status) {
                    // 判断在线状态，如果为被其他端踢掉，做登出操作
                    if (status == StatusCode.KICKOUT) {
                        //被挤下线
                        sendBroadcastKickout();
                    }
                }
            }, true);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void sendBroadcastKickout() {
        Intent intent = new Intent();
        intent.setAction(AppConstant.BROADCAST_LOGIN);
        sendBroadcast(intent);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
